//
//  YLMemberLevelViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/17.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMemberLevelViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLMemberLevelModel.h"
#import <YYModel.h>
@implementation YLMemberLevelViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) wealSelf = self;
    _requestCommend = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCustomerLevel", YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    YLMemberLevelModel *model = [YLMemberLevelModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                    
                    // 如果是最高等级
                    if (model.isHighestLevel) {
                        model.showMoney =  model.showYearInterestRate;
                        CGFloat maxRate = 50000000;
                        // 80000000
                        if (model.yearInterestRate < maxRate / 8.0) {
                            model.rateProgress = model.yearInterestRate / (maxRate / 8.0) / 4.0;
                        } else if (model.yearInterestRate < maxRate / 4.0) {
                            model.rateProgress = model.yearInterestRate / (maxRate / 4.0) / 4.0 + 0.25;
                        } else if (model.yearInterestRate < maxRate / 2.0) {
                            model.rateProgress = model.yearInterestRate / (maxRate / 2.0) / 4.0 + 0.5;
                        } else {
                            model.rateProgress = model.yearInterestRate / (maxRate) / 4.0 + 0.75;
                        }
                        
                    } else {
                        model.rateProgress = model.yearInterestRate / (model.nextInterestRate * 1.0);
                        model.showMoney = [NSString stringWithFormat:@"%@/%@", model.showYearInterestRate, model.showNextInterestRate];
                    }
                    
                    
                    switch (model.levelID) {
                            case 1:
                            model.showTitle = @"升级到VIP特权,享受更多特权>";
                            break;
                        
                            case 2:
                            
                            model.showTitle = @"升级到SVIP会员,享受更多特权>";
                            break;
                        
                            case 3:
                            model.showTitle = @"升级到尊享会员,享受更多特权>";
                            break;
                            
                            case 4:
                            model.showTitle = @"更多特权,敬请期待";
                            
                        default:
                            break;
                    }
                    
                    
                    
                    wealSelf.levelModel = model;
                    [subscriber sendNext:nil];
                    
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                
                [subscriber sendCompleted];
            } fail:^(NSError *error) {
                [subscriber sendCompleted];
            }];
                return nil;
        }];
    }];
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

@end
